package com.course.${module}.controller.admin;

import com.course.business.controller.BaseController;
import com.course.model.dto.${Entity}Dto;
import com.course.model.dto.PageDto;
import com.course.common.response.BaseResponse;
import com.course.model.util.ValidatorUtil;
import com.course.model.service.${Entity}Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/${entity}")
public class ${Entity}Controller extends BaseController{

    public static final String BUSINESS_NAME = "${tableNameCn}";

    @Resource
    private ${Entity}Service ${entity}Service;

    /**
     * 列表查询
     */
    @PostMapping("/get${Entity}List")
    public BaseResponse list(@RequestBody PageDto pageDto) {
        BaseResponse response = new BaseResponse().sucess();
        ${entity}Service.list(pageDto);
        response.setData(pageDto);
        return response;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public BaseResponse save(@RequestBody ${Entity}Dto ${entity}Dto) {
        // 保存校验
        <#list fieldList as field>
        <#if field.name!="id" && field.nameHump!="createdAt" && field.nameHump!="updatedAt" && field.nameHump!="sort">
            <#if !field.nullAble>
        ValidatorUtil.require(${entity}Dto.get${field.nameBigHump}(), "${field.nameCn}");
            </#if>
            <#if (field.length > 0)>
        ValidatorUtil.length(${entity}Dto.get${field.nameBigHump}(), "${field.nameCn}", 1, ${field.length?c});
            </#if>
        </#if>
        </#list>

        BaseResponse response = new BaseResponse().sucess();
        ${entity}Service.save(${entity}Dto);
        response.setData(${entity}Dto);
        return response;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse delete(@PathVariable String id) {
        BaseResponse response = new BaseResponse().sucess();
        ${entity}Service.delete(id);
        return response;
    }
}
